package com.aide.simplification.popedom.popedom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

import com.aide.simplification.global.Utils;
import com.aide.simplification.popedom.popedom.base.BasePop;
import com.aide.simplification.popedom.popedom.base.PopedomOP;

/**
 * 角色管理
 * 
 * @author sorc
 * 
 */
//@Component("Role")
//@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "Role"), name = "Role")
@RemoteProxy(name="Role") 
public class Role extends BasePop {

	/**
	 * 分页查询所有角色
	 * 
	 * @param page
	 *            页号
	 * @param size
	 *            每页几条
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> findAllRolePage(int page, int size) {
		String sql = dbop.initPageSql(page, size, "roleid",
				"select * from pop_role");
		return dbop.queryForList(sql);
	}

	/**
	 * 分页查询所有可用角色
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> findAllRolePageIsUse(int page, int size) {
		String sql = dbop.initPageSql(page, size, "roleid",
				"select * from pop_role where sign=1");
		return dbop.queryForList(sql);
	}

	/**
	 * 分页查询某用户所有角色
	 * 
	 * @param useruuid
	 * @param page
	 * @param size
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> findAllRolePageIsUseByUser(
			String useruuid, int page, int size) {
		String sql = dbop
				.initPageSql(
						page,
						size,
						"roleid",
						"select DISTINCT r.roleid as roleid,r.rolename as rolename,r.roleLevel as roleLevel,r.roletext as roletext from pop_role as r,pop_dept_role as dr,pop_user_dept as ud where ud.deptid=dr.deptid and dr.roleid=r.roleid and ud.useruuid='"
								+ useruuid + "' and r.sign=1");
		return dbop.queryForList(sql);
	}

	/**
	 * 分页查询某部门所有角色 在用
	 * 
	 * @param deptid
	 * @param page
	 * @param size
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> findAllRolePageIsUseByDept(String deptid,
			int page, int size) {
		String sql = dbop
				.initPageSql(
						page,
						size,
						"roleid",
						"select r.roleid as roleid,r.rolename as rolename,r.roleLevel as roleLevel,r.roletext as roletext from pop_role as r,pop_dept_role as dr where dr.roleid=r.roleid and dr.deptid='"
								+ deptid + "' and r.sign=1");
		return dbop.queryForList(sql);
	}

	/**
	 * 检查 角色名是否已经存在 在用
	 * 
	 * @param pname
	 * @return
	 */
	@RemoteMethod
	public int checkPName(String rolename, String roleid) {
		if (rolename.indexOf("'") != -1 || rolename.length() <= 0) {
			return 0;
		}
		String sql = "select count(roleid) from pop_role where  rolename=? and roleid!=?";
		if (dbop.queryForInt(sql,new Object[]{rolename,roleid}) > 0) {
			return 0;
		}
		return 1;
	}

	/**
	 * 按照角色主键删除
	 * 
	 * @param roleid
	 * @return
	 */
	@RemoteMethod
	public String removeRoleById(String roleid) {
		// odb.autoDelete("pop_user_role", "roleid", roleid);
		// odb.autoDelete("pop_dept_role", "roleid", roleid);
		UserRole ur = new UserRole();
		if (ur.findByRoleid(roleid).size() == 0) {
			DeptRole dr = new DeptRole();
			if (dr.findByRoleid(roleid).size() == 0) {
				int flag = dbop.delete("pop_role", "roleid", roleid);
				if (flag == 1) {
					return "删除成功";
				}
			} else {
				return "请先移除部门！";
			}
		} else {
			return "请先移除用户！";
		}
		return "删除失败";
	}

	/**
	 * 按照组织机构分页查询用户
	 * 
	 * @param deptid
	 * @param page
	 * @param size
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> findAllUserPageByRole(String roleid,
			int page, int size) {
		User user = new User();
		return user.findAllUserPageByRole(page, size, roleid);
	}

	/**
	 * 按照组织机构统计用户数量
	 * 
	 * @param deptid
	 * @return
	 */
	@RemoteMethod
	public int findAllUserPageByRoleGetCount(String roleid) {
		String sql = "SELECT count(*) FROM pop_user_role WHERE roleid = ?";
		return dbop.queryForInt(sql,new Object[]{roleid});
	}

	/**
	 * 按照用户编号和角色编号删除用户-角色关系
	 * 
	 * @param useruuid
	 * @param roleid
	 * @return
	 */
	@RemoteMethod
	public int removeByUserUUIDRoleid(String useruuid, String roleid) {
		UserRole ur = new UserRole();
		return ur.removeByUserUUIDRoleid(useruuid, roleid);
	}

	/**
	 * 按主键查询角色信息
	 * 
	 * @param pkvalue
	 * @return
	 */
	@RemoteMethod
	public Map<String, Object> findByPk(String pkvalue) {
		return dbop.findByPK("pop_role", "roleid", pkvalue);
	}

	/**
	 * 按照角色编号查询权限编号
	 * 
	 * @param roleid
	 * @return
	 */
	@RemoteMethod
	public String[] findPopsByRoleid(String roleid) {
		PopedomOP op = new PopedomOP();
		String pop = "0";
		try {
			pop = this.findByPk(roleid).get("pop").toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		int[] popids = op.getPops36(pop);
		String[] pops = new String[popids.length];

		for (int i = 0; i < popids.length; i++) {
			pops[i] = "" + popids[i];
		}
		return pops;
	}

	/**
	 * 查询所有可用角色
	 * 
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> findAllRoleIsUse() {
		String sql = "select * from pop_role where sign=1";
		return dbop.queryForList(sql);
	}

	/**
	 * 查询角色总数
	 * 
	 * @return
	 */
	@RemoteMethod
	public int getCount() {
		return dbop.queryForInt("select count(*) from pop_role");
	}

	/**
	 * 查询可用角色总数
	 * 
	 * @return
	 */
	@RemoteMethod
	public int getCountIsUse() {
		return dbop.queryForInt("select count(*) from pop_role where sign=1");
	}

	/**
	 * 查询站点某人可用角色总数
	 * 
	 * @return
	 */
	@RemoteMethod
	public int getCountIsUseBySUser(String useruuid) {
		String sql = "select count(*) from pop_role as r,pop_dept_role as dr,pop_user_dept as ud where ud.deptid=dr.deptid and dr.roleid=r.roleid and ud.useruuid=? and r.sign=1";
		return dbop.queryForInt(sql,new Object[]{useruuid});
	}

	/**
	 * 查询站点某人可用角色总数
	 * 
	 * @return
	 */
	@RemoteMethod
	public int getCountIsUseByDept(String deptid) {
		String sql = "select count(*) from pop_role as r,pop_dept_role as dr where dr.roleid=r.roleid and dr.deptid=? and r.sign=1";
		return dbop.queryForInt(sql,new Object[]{deptid});
	}

	/**
	 * 为角色添加/修改 权限 需要及联修改用户和部门的pop 以实现（在方法updateRole）
	 * 
	 * 
	 * @param roleid
	 * @param pops
	 * @param nopops
	 * @return
	 */
	@RemoteMethod
	public int addPopedoms(String roleid, String pops[], String nopops[]) {
		PopedomOP op = new PopedomOP();
		String pop = "0";
		try {
			pop = this.findByPk(roleid).get("pop").toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		pop = op.unitePop36(pop, op.getPop36(pops));
		String popString = op.reducePop36(pop, op.getPop36(nopops));
		return this.updateRoleAuto(roleid, popString);

	}

	/**
	 * 新建角色 在用
	 * 
	 * @param role
	 * @param roletext
	 * @param sign
	 * @param pop
	 * @return
	 */
	@RemoteMethod
	public int newRole(String role, String roletext, String sign,
			String roleLevel) {
		Map<String, Object> hm = new HashMap<String, Object>();
		hm.put("roleid", Utils.getUUID());
		hm.put("rolename", role);
		hm.put("roletext", roletext);
		hm.put("sign", sign);
		hm.put("pop", "0");
		hm.put("roleLevel", roleLevel);
		hm.put("version", "0");
		return dbop.save("pop_role", hm);
	}

	/**
	 * 更新角色 在用
	 * 
	 * @param roleid
	 * @param role
	 * @param roletext
	 * @param sign
	 * @param pop
	 * @return
	 */
	@RemoteMethod
	public int updateRole(String roleid, String role, String roletext,
			String sign, String roleLevel) {
		int flag = 0;
		Map<String, Object> hm = new HashMap<String, Object>();
		hm.put("rolename", role);
		hm.put("roletext", roletext);
		hm.put("sign", sign);
		hm.put("roleLevel", roleLevel);
		hm.put("version", "0");
		flag = dbop.update("pop_role", hm, "roleid", roleid);
		if (flag == 1) {
			// 更新角色等级及联更新人员等级和部门等级
			String sqluserLevel = "update pop_user set pop_user.userLevel=(";
			sqluserLevel += "select max(pr2.roleLevel) from pop_user_role as pur2,pop_role as pr2 where pur2.roleid=pr2.roleid and pur2.useruuid=pop_user.useruuid)";
			sqluserLevel += " where pop_user.useruuid in (select pur1.useruuid from pop_user_role as pur1 where pur1.roleid='"
					+ roleid + "')";
			dbop.executeUpdate(sqluserLevel);
			String sqldeptLevel = "update pop_dept set pop_dept.deptLevel=(";
			sqldeptLevel += "select max(pr2.roleLevel) from pop_dept_role as pdr2,pop_role as pr2 where pdr2.roleid=pr2.roleid and pdr2.deptid=pop_dept.deptid)";
			sqldeptLevel += " where pop_dept.deptid in (select pdr1.deptid from pop_dept_role as pdr1 where pdr1.roleid='"
					+ roleid + "')";
			dbop.executeUpdate(sqldeptLevel);
		}
		return flag;
	}

	/**
	 * 按照角色编号更新权限为pop
	 * 
	 * @param roleid
	 * @param pop
	 * @return
	 */
	@RemoteMethod
	public int updateRoleAuto(String roleid, String pop) {
		int flag = 0;
		Map<String, Object> hm = new HashMap<String, Object>();
		hm.put("pop", pop);
		flag = dbop.update("pop_role", hm, "roleid", roleid);
		if (flag == 1) {
			UserRole ur = new UserRole();// 及联更新与本角色有关的人员的权限串
			if (ur.updatePopByRole(roleid) >= 0) {
				flag = 1;
			} else {
				return 0;
			}
			DeptRole dr = new DeptRole();
			if (dr.updatePopByRole(roleid) >= 0) {// 及联更新与本角色有关的组织机构的权限串
				flag = 1;
			} else {
				return 0;
			}
		}
		return flag;
	}

	/**
	 * 查看角色信息按照主键 在用
	 * 
	 * @param pkvalue
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> chakan(String pkvalue) {
		Map<String, Object> hm = findByPk(pkvalue);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (hm != null) {
			Map<String, Object> hm0 = new HashMap<String, Object>();
			hm0.put("title", "角色编号:");
			hm0.put("tname", "roleid");
			hm0.put("tvalue", hm.get("roleid").toString());
			list.add(hm0);

			Map<String, Object> hm1 = new HashMap<String, Object>();
			hm1.put("title", "角色名称:");
			hm1.put("tname", "rolename");
			hm1.put("tvalue", hm.get("rolename").toString());
			list.add(hm1);

			Map<String, Object> hm2 = new HashMap<String, Object>();
			hm2.put("title", "角色描述:");
			hm2.put("tname", "roletext");
			hm2.put("tvalue", hm.get("roletext").toString());
			list.add(hm2);

			Map<String, Object> hm3 = new HashMap<String, Object>();
			hm3.put("title", "角色等级:");
			hm3.put("tname", "rolelevel");
			hm3.put("tval", hm.get("rolelevel").toString());
			hm3.put("tvalue", hm.get("rolelevel").toString());
			list.add(hm3);

			Map<String, Object> hm4 = new HashMap<String, Object>();
			hm4.put("title", "有效标记:");
			hm4.put("tname", "sign");
			hm4.put("tval", hm.get("sign"));
			if (hm.get("sign").toString().equals("1")) {
				hm4.put("tvalue", "可用");
			} else {
				hm4.put("tvalue", "不可用");
			}
			list.add(hm4);
		}
		return list;
	}

	/**
	 * 为组织机构 添加/修改 角色
	 * 
	 * @param roleid
	 * @param deptids
	 * @return
	 */
	@RemoteMethod
	public int addDepts(String roleid, String deptids) {
		DeptRole dr = new DeptRole();
		dr.removeByRoleid(roleid);
		int flag = 0;
		if (deptids.equals("")) {
			return 1;
		} else {
			String[] deptid = deptids.split(",");
			for (int j = 0; j < deptid.length; j++) {
				String dept = deptid[j];
				flag += dr.newDeptRole(roleid, dept);
			}
			if (flag == deptid.length) {
				flag = 1;
			}
		}
		dr.updatePopByRole(roleid);
		return flag;
	}

	/**
	 * 按照角色编号查询部门编号
	 * 
	 * @param roleid
	 * @return
	 */
	@RemoteMethod
	public List<Map<String, Object>> findDeptidByRoleid(String roleid) {
		DeptRole dr = new DeptRole();
		return dr.findByRoleid(roleid);
	}
}
